package com.luntan.school.dao.impl;

import com.luntan.school.dao.UserDao;
import com.luntan.school.model.User;
import com.luntan.school.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaolmpl implements UserDao {
    @Override
    public User login(String account, String password) {
        try {
            //获取连接
            Connection  connection = DBUtil.getConnection();
            //执行查询并返回结果集
            PreparedStatement preparedStatement = connection.prepareStatement("select * from user where (account = ? or mobile=? or email=?) and password = ? and forbidden=?");
            preparedStatement.setString(1, account);
            preparedStatement.setString(2, account);
            preparedStatement.setString(3, account);
            preparedStatement.setString(4, password);
            preparedStatement.setBoolean(5, false);

            //执行查询操作，将查询结果封装在ResultSet对象中返回
            ResultSet resultSet = preparedStatement.executeQuery();

            User  user = null;
            //处理结果集
            if(resultSet.next()){
                user = new User();
                user.setAccount(account);
                user.setId(resultSet.getInt("id"));
//                user.setBirthday(resultSet.getDate("birthday"));
                user.setMobile(resultSet.getString("mobile"));
                user.setEmail(resultSet.getString("email"));
                user.setPhoto(resultSet.getString("photo"));
                user.setNickname(resultSet.getString("nickname"));
            }
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return user;
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean addUser(User user) {
        try {
            //获取连接
            Connection connection = DBUtil.getConnection();

            PreparedStatement preparedStatement = connection.prepareStatement("insert into user(account,nickname,password,photo,email,mobile,forbidden) values (?,?,?,?,?,?,?)");

            preparedStatement.setString(1, user.getAccount());
            preparedStatement.setString(2, user.getNickname());
            preparedStatement.setString(3, user.getPassword());
            preparedStatement.setString(4, user.getPhoto());
//            preparedStatement.setDate(5, new Date(user.getBirthday().getTime()));
            preparedStatement.setString(5, user.getEmail());
            preparedStatement.setString(6, user.getMobile());
            preparedStatement.setBoolean(7, false);//默认不禁用

            int i = preparedStatement.executeUpdate();

            preparedStatement.close();
            connection.close();

            return i > 0; //>0表示成功创建一行
        }catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean isUserExist(String key, String value) {
        try {
            Connection connection = DBUtil.getConnection();

            PreparedStatement preparedStatement = connection.prepareStatement("select * from user where " + key + " = ?");
            preparedStatement.setString(1, value);

            ResultSet resultSet = preparedStatement.executeQuery();
            boolean result = resultSet.next();//判断是否查询到数据

            resultSet.close();
            connection.close();
            preparedStatement.close();
            return result;
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<User> getAllUsers() {
        try {
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from user");

            //以封装结果为列表数据
            List<User> users = new ArrayList<>();
            while(resultSet.next()){
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setAccount(resultSet.getString("account"));
                user.setNickname(resultSet.getString("nickname"));
                user.setPhoto(resultSet.getString("photo"));
//                user.setBirthday(resultSet.getTimestamp("birthday"));
                user.setEmail(resultSet.getString("email"));
                user.setForbidden(resultSet.getBoolean("forbidden"));
                user.setMobile(resultSet.getString("mobile"));

                users.add(user);
            }
            resultSet.close();
            statement.close();
            connection.close();

            return users;//返回结果
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void forbiddenUser(int userId, boolean forbidden) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("update user set forbidden = ? where id = ?");
            preparedStatement.setBoolean(1, forbidden);
            preparedStatement.setInt(2, userId);

            preparedStatement.executeUpdate();

            preparedStatement.close();
            connection.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void upDatePassword(String account, String password) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("update user set password= ? where account = ?");

            preparedStatement.setString(1, password);
            preparedStatement.setString(2, account);

            preparedStatement.executeUpdate();

            preparedStatement.close();
            connection.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateUserInfo(User user) {

        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("update user set nickname = ?, photo = ?, birthday = ?, email = ?, mobile=? where account = ?");
            preparedStatement.setString(1, user.getNickname());
            preparedStatement.setString(2, user.getPhoto());
//            preparedStatement.setDate(3, new Date(user.getBirthday().getTime()));
            preparedStatement.setString(3, user.getEmail());
            preparedStatement.setString(4, user.getMobile());
            preparedStatement.setString(5, user.getAccount());

            preparedStatement.executeUpdate();

            preparedStatement.close();
            connection.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        UserDao userDao = new UserDaolmpl();
        System.out.println(userDao.login("chazhong996","123456"));
        System.out.println(userDao.isUserExist("account","chazhong996"));
    }


}
